﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Telerik.Windows.Data;
using Conqueror.WP.ViewModel.Matchmaking;
using Telerik.Windows.Controls;

namespace Conqueror.WP
{
    public partial class MatchmakingPage : PhoneApplicationPage
    {
        public MatchmakingPage()
        {
            InitializeComponent();

            this.radJumpList.VirtualizationStrategyDefinition = new JumpListWrapVirtualizationStrategyDefinition() { Orientation = System.Windows.Controls.Orientation.Horizontal };

            string alphabet = "#abcdefghijklmnopqrstuvwxyz";
            List<string> groupPickerItems = new List<string>(32);
            foreach (char c in alphabet)
            {
                groupPickerItems.Add(new string(c, 1));
            }
            this.radJumpList.GroupPickerItemsSource = groupPickerItems;

            GenericGroupDescriptor<MapViewModel, string> groupByFirstName = new GenericGroupDescriptor<MapViewModel, string>(map =>
                {
                    int number;
                    string m = map.Name.Substring(0, 1);
                    if (Int32.TryParse(m, out number))
                        return "#";
                    else return m.ToLower();
                });
            this.radJumpList.GroupDescriptors.Add(groupByFirstName);

            HideFiltersStory.Begin();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (filtersOverlay.Visibility == System.Windows.Visibility.Visible) //If showing the filter than hide it and don't go back a page
            {
                HideFiltersStory.Begin();
                e.Cancel = true;
            }
            else
            {
                base.OnBackKeyPress(e);
            }
        }

        private void FindGamesButtonClick(object sender, RoutedEventArgs e)
        {
            if (ValidateMatchmakingData(MatchmakingViewModel.Instance))
                NavigationService.Navigate(new Uri("/MatchmakingGamesPage.xaml", UriKind.Relative));
        }

        private bool ValidateMatchmakingData(MatchmakingViewModel matchmakingViewModel)
        {
            if (matchmakingViewModel.MaxPlayers < matchmakingViewModel.MinPlayers)
            {
                MessageBox.Show("You may not have a maximum number of players lower than the minimum number of players.");
                return false;
            }
            return true;
        }

        private void CancelFiltersClick(object sender, RoutedEventArgs e)
        {
            HideFiltersStory.Begin();
        }

        private void SelectMapTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var map = (sender as FrameworkElement).DataContext as ViewModel.Matchmaking.MapViewModel;
            ViewModel.Matchmaking.MatchmakingViewModel.Instance.SelectedMap = map;
            ShowFiltersStory.Begin();
        }

        private void InspectMapClick(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri(string.Format("/MapInspectorPage.xaml?map={0}", ViewModel.Matchmaking.MatchmakingViewModel.Instance.SelectedMap.Name), UriKind.Relative));
        }

        private void radJumpList_GroupPickerItemTap(object sender, Telerik.Windows.Controls.GroupPickerItemTapEventArgs e)
        {
            foreach (DataGroup group in this.radJumpList.Groups)
            {
                if (string.Equals(e.DataItem, group.Key))
                {
                    e.DataItemToNavigate = group;
                    return;
                }
            }
            e.ClosePicker = false;
            e.DataItemToNavigate = this.radJumpList.Groups[0];
        }
    }
}